﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MyOA.Model;

namespace MyOA.DAL
{
    public class DistributeRoleService
    {
        DBHelper db = new DBHelper();

        /// <summary>
        /// 获取全部的菜单信息
        /// </summary>
        /// <returns>返回全部的菜单信息</returns>
        public IList<SysFun> getAllSysFun()
        {
            IList<SysFun> sysFuns = new List<SysFun>();
            using (SqlDataReader reader = db.getDataReader("sp_getAllSysFun"))
            {
                while (reader.Read())
                {
                    SysFun sysFun = new SysFun();

                    sysFun.NodeId = (int)reader[0];
                    sysFun.DisplayName = (string)reader[1];
                    sysFun.NodeURL = (string)reader[2];
                    sysFun.DisplayOrder = (int)reader[3];
                    sysFun.ParentNodeId = (int)reader[4];

                    sysFuns.Add(sysFun);
                }
            }
            db.CloseConnection();
            return sysFuns;
        }

        /// <summary>
        /// 根据角色名获取权限信息集合
        /// </summary>
        /// <param name="roleName">角色名</param>
        /// <returns>返回权限信息</returns>      
        public IList<SysFun> getSysFunByRoleName(string roleName)
        {
            IList<SysFun> sysFuns = new List<SysFun>();
            using (SqlDataReader reader = db.getDataReader("sp_getSysFunByRoleName", new SqlParameter("@roleName", roleName)))
            {
                while (reader.Read())
                {
                    SysFun sysFun = new SysFun();

                    sysFun.NodeId = (int)reader[0];
                    sysFun.DisplayName = (string)reader[1];
                    //sysFun.NodeURL = (string)reader[2];
                    //sysFun.DisplayOrder = (int)reader[3];
                    sysFun.ParentNodeId = (int)reader[4];

                    sysFuns.Add(sysFun);
                }
            }
            db.CloseConnection();
            return sysFuns;
        }

        /// <summary>
        /// 根据角色ID来获取此角色拥有的权限ID集合
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <returns>返回此角色拥有的权限ID集合</returns>
        public IList<RoleRight> getRoleRightByRoleId(int roleId)
        {
            IList<RoleRight> roleRights = new List<RoleRight>();
            using (SqlDataReader reader = db.getDataReader("sp_getRoleRightByRoleId", new SqlParameter("@roleId", roleId)))
            {
                while (reader.Read())
                {
                    RoleRight roleRight = new RoleRight();

                    roleRight.Node.NodeId = (int)reader[0];
                    roleRights.Add(roleRight);
                }
            }
            db.CloseConnection();
            return roleRights;
        }

        /// <summary>
        /// 增加或删除指定角色的权限
        /// </summary>
        /// <param name="roleId">角色ID</param>    
        /// <param name="deleteNodeId">删除的权限ID字符串</param>
        /// <param name="insertNodeId">增加的权限ID字符串</param>
        /// <returns>是否增加或删除成功</returns>
        public bool insertAndRoleRight(int roleId, string deleteNodeId, string insertNodeId)
        {
            SqlParameter[] value = new SqlParameter[]
           {
               new SqlParameter("@roleId",roleId),
               new SqlParameter("@insertNodeId",insertNodeId),
               new SqlParameter("@deleteNodeId",deleteNodeId)
           };

            if (db.getCount("sp_insertAndDeleteRoleRight", value) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
